﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SharedClientDll;
using Shared.Model;
using Client;

namespace ManageSystem.Manager
{
    public partial class FormInputData : UserControl
    {

        private List<BOB_TYPE> _listBobType = null;

        public FormInputData(int choice)
        {
            InitializeComponent();

            if (choice == 0)
            {
                //选中Device_Data
                this.comboBoxTableName.SelectedIndex = 0;
            }
            else
            {
                //选中BOSA_DATA
                this.comboBoxTableName.SelectedIndex = 1;
            }
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.ShowDialog();

            if (open.FileName.Contains(".xlsx"))
                this.textBoxExcelPath.Text = open.FileName;
            else
                MessageBox.Show("请选择以.xlsx结尾的文件");
        }

        //导入数据
        private void ButtonExcelInput_Click(object sender, EventArgs e)
        {
            string excelPath = this.textBoxExcelPath.Text;

            if (string.IsNullOrEmpty(excelPath))
            {
                MessageBox.Show("Excel文件路径为空");
                return;
            }

            string msg = "";

            //导入数据
            if (this.comboBoxTableName.SelectedIndex == 0)
            {
                #region 使用订单号代表省份和类型，订单数量根据上传的MAC地址动态变化

                YcdExcelBaseClass<Device_Data> excelObj = new YcdExcelBaseClass<Device_Data>();
                if (excelObj.InitExcelClass(excelPath, out msg) == false)
                {
                    MessageBox.Show("初始化Excel文档失败：" + msg);
                    return;
                }

                //判断数据是否完整
                if (excelObj.FirstRow.Contains("DeviceType") == false
                    || excelObj.FirstRow.Contains("PONType") == false
                    //|| excelObj.FirstRow.Contains("HW_PROVINCE_NAME") == false
                    //|| excelObj.FirstRow.Contains("WIFI_SSID") == false
                    //|| excelObj.FirstRow.Contains("WIFI_Password") == false
                    //|| excelObj.FirstRow.Contains("CMIIT_ID") == false
                    )
                {
                    //MessageBox.Show("请检查第一行表头数据是否缺失(DeviceType、PONType、HW_PROVINCE_NAME、WIFI_SSID、WIFI_Password、CMIIT_ID、LightModule)");
                    MessageBox.Show("请检查第一行表头数据是否缺失(DeviceType、PONType)");
                    return;
                }

                //InsertDeviceDataSecond(excelSecond);

                InsertDeviceDataThird(excelObj);

                return;
                #endregion

            }
            else if (this.comboBoxTableName.SelectedIndex == 1)
            {
                //if (string.IsNullOrEmpty(this.comboBoxBosaCompany.Text))
                //{
                //    MessageBox.Show("请选择BOSA芯片厂商");
                //    return;
                //}

                YcdExcelBaseClass<BOSA_DATA> excel = new YcdExcelBaseClass<BOSA_DATA>();

                if (excel.InitExcelClass(excelPath, out msg) == false)
                {
                    MessageBox.Show("初始化Excel文档失败：" + msg);
                    return;
                }

                if (excel.FirstRow.Contains("DeviceType") == false)
                {
                    MessageBox.Show("请检查第一行表头数据是否缺失(DeviceType)");
                    return;
                }

                InsertBosaDataData(excel);
            }
        }

        private bool GetOrderMsg(out int orderNumber, out int hadMacNumber,string orderSn,out YCD_ORDER order)
        {
            orderNumber = 0;
            hadMacNumber = 0;

            order = null;

            YCD_ORDER orderW = new YCD_ORDER();
            orderW.SetWhere();
            orderW.ORDER_SN = orderSn;

            var selectOrder = MethodWcf.SelectOrderClient(orderW);
            if (selectOrder.result == false)
            {
                MessageBox.Show("未能查到订单信息");
                return false;
            }

            orderNumber = selectOrder.value[0].NUMBER;
            order = selectOrder.value[0];

            Device_Data device = new Device_Data();
            device.SetWhere();
            device.ORDER_SN = orderSn;

            var selectCountDevice = MethodWcf.SelectDeviceDataCountClient(device);
            if (selectCountDevice.result == false)
            {
                MessageBox.Show("查询已导入订单的MAC数量失败");
                return false;
            }

            hadMacNumber = int.Parse(selectCountDevice.message);

            return true;
        }

        private void InsertBosaDataData(YcdExcelBaseClass<BOSA_DATA> excel)
        {
            Task.Factory.StartNew(() =>
            {
                SetEnable(false);

                int total = 0, pass = 0;

                //判断数据库中是否有了数据
                BOSA_DATA bosaWhere = new BOSA_DATA();
                        
                BOSA_DATA bosa = null;

                DateTime start = DateTime.Now;

                StringBuilder sqlBuf = new StringBuilder();
                int sqlCount = 0;

                sqlBuf.AppendLine(SharedData.SQL_TRANSCATION_START);

                while (true)
                {
                    //continue;
                    //判断是否有相应的BOSA信息

                    #region using transcation

                    bosa = excel.GetRow(out string msg);

                    //get the type of bob
                    //bosa.BOB_TYPE_ID = (from t in _listBobType
                    //                    where t.BG_MAX > bosa.Imo_mA * 1000 && t.BG_MIN <= bosa.Imo_mA * 1000
                    //                    && t.COMPANY == this.comboBoxBosaCompany.Text.Trim()
                    //                    && t.POWER_MAX > bosa.Po_dBm && t.POWER_MIN <= bosa.Po_dBm
                    //                    select t.ID).FirstOrDefault();

                    if (sqlCount >= 50 || (bosa == null && sqlCount > 0))
                    {
                        sqlBuf.AppendLine(SharedData.SQL_TRANSCATION_END);
                        int executeNum = MethodWcf.ExecuteSQLClient(sqlBuf.ToString());

                        //out put result message
                        if (executeNum <= 0)
                        {
                            AppendText(this.richTextBoxFail, sqlBuf.ToString());
                        }
                        else
                        {
                            pass += executeNum;
                        }

                        total += sqlCount;
                        SetText(labelPass, pass.ToString());
                        SetText(labelTotal, total.ToString());

                        //reset properties
                        sqlCount = 0;
                        sqlBuf.Clear();

                        sqlBuf.AppendLine(SharedData.SQL_TRANSCATION_START);
                    }

                    if (bosa == null) break;

                    bosaWhere.Clear();
                    bosaWhere.SetWhere();
                    bosaWhere.SN = bosa.SN;

                    sqlBuf.AppendLine($"IF ({Shared.Method.ObjectToSQL.ObjectToSelectCountSql(bosaWhere)})=0 BEGIN");
                    sqlBuf.AppendLine(Shared.Method.ObjectToSQL.ObjectToInsertSql(bosa));
                    sqlBuf.AppendLine("END ELSE BEGIN");
                    bosa.SetWhere();
                    bosa.SN = bosa.SN;
                    sqlBuf.AppendLine(Shared.Method.ObjectToSQL.ObjectToUpdateSql(bosa));
                    sqlBuf.AppendLine("END");

                    ++sqlCount;

                    #endregion

                    continue;

#if false
                    bosaWhere.SetWhere();//用于发现BOSA是否存在
                    bosaWhere.SN = bosa.SN;
                    var selectBosaCount = MethodWcf.SelectBosaDataCountClient(bosaWhere);

                    ResultSet updateBosa = null;
                    if (selectBosaCount.result && selectBosaCount.message == "1")
                    {
                        bosa.SetWhere();
                        bosa.SN = bosa.SN;
                        updateBosa = MethodWcf.UpdateBosaDataClient(bosa);
                    }
                    else
                    {
                        updateBosa = MethodWcf.InsertBosaDataClient(bosa);
                    }

                    if (updateBosa.result)
                    {
                        //AppendText(this.richTextBoxPass, "导入成功,SN：" + bosa.SN);
                        pass++;
                    }
                    else
                    {
                        AppendText(this.richTextBoxFail, "导入失败,SN：" + bosa.SN + ",错误信息：" + msg + updateBosa.message);
                    }
                    total++;

                    SetText(labelPass, pass.ToString());
                    SetText(labelTotal, total.ToString());
#endif
                }

                TimeSpan runTime= DateTime.Now - start;

                MessageBox.Show($"run time is: {runTime.TotalSeconds}s");

                //写回显信息
                SetEnable(true);
            });
        }

        private void InsertDeviceData(YcdExcelBaseClass<Device_Data> excel, int orderNumber, int hadMacNumber,string orderSn,YCD_ORDER order)
        {
            Task.Factory.StartNew(() =>
            {
                SetEnable(false);

                int total = 0, pass = 0;

                //判断Mac是否已经上传过了的变量
                Device_Data deviceWhere = new Device_Data();

                Device_Data device = null;
                while ((device = excel.GetRow(out string msg)) != null)
                {
                    //判断ORDER的信息与数据信息是否一致
                    if (device.DeviceType != order.DEVICE_TYPE || device.ProductCode != order.PRODUCT_CODE || device.HW_PROVINCE_NAME != order.PROVINCE)
                    {
                        MessageBox.Show(string.Format("上传数据与订单信息不一致,MAC[{0}]", device.MACAddr));
                        return;
                    }

                    //判断MAC是否已经上传成功了
                    deviceWhere.SetWhere();
                    //deviceWhere.MACAddr = device.MACAddr;
                    //deviceWhere.ORDER_SN = orderSn;//主要剔除掉MAC在另外一个订单中的情况，有SN号码的不能更新
                    deviceWhere.SetWhere(string.Format("MACAddr ='{0}' AND ORDER_SN='{1}' AND SN is null",device.MACAddr,orderSn));
                    var selectDeviceCount = MethodWcf.SelectDeviceDataCountClient(deviceWhere);

                    //修改ITV_SSID和ITV_Password
                    device.ITV_Password = device.WIFI_Password;
                    device.ITV_SSID = "iTV" + device.WIFI_SSID.Substring(8);

                    device.date1 = DateTime.Now.Date;
                    device.ORDER_SN = orderSn;

                    //更新或者写入DEVICE
                    ResultSet updateDeviceData = null;

                    //判断数据是否已经存在
                    if (selectDeviceCount.result && selectDeviceCount.message == "1")
                    {
                        //更新
                        device.SetWhere();
                        device.MACAddr = device.MACAddr;

                        updateDeviceData = MethodWcf.UpdateDeviceDataClient(device);
                    }
                    else
                    {
                        if (orderNumber == hadMacNumber)
                        {
                            MessageBox.Show("MAC已经上传完毕，不能继续上传");
                            break;
                        }

                        //插入
                        updateDeviceData = MethodWcf.InsertDeviceDataClient(device);
                        //已有的MAC增加1
                        if (updateDeviceData.result) hadMacNumber++;
                    }

                    if (updateDeviceData.result)
                    {
                        AppendText(this.richTextBoxPass, "导入成功,MAC：" + device.MACAddr);
                        pass++;
                    }
                    else
                    {
                        AppendText(this.richTextBoxFail, "导入失败,MAC：" + device.MACAddr+",错误信息："+msg+updateDeviceData.message);
                    }

                    //写回显示信息
                    total++;

                    SetText(labelPass, pass.ToString());
                    SetText(labelTotal, total.ToString());
                }

                SetEnable(true);
            });
        }

        /// <summary>
        /// 第二种上传MAC的方案
        /// </summary>
        /// <param name="excel"></param>
        private void InsertDeviceDataSecond(YcdExcelBaseClass<Device_Data> excel)
        {
            Task.Factory.StartNew(() =>
            {
                SetEnable(false);

                DateTime start = DateTime.Now;
                int total = 0, pass = 0;

                //订单信息
                YCD_ORDER order = null;

                //判断Mac是否已经上传过了的变量
                Device_Data deviceWhere = new Device_Data();

                Device_Data device = null;

                while ((device = excel.GetRow(out string msg)) != null)
                {
                    //判断数据与order是否是同类型的
                    if (order == null)
                    {
                        order = new YCD_ORDER();
                        order.SetWhere();
                        order.ORDER_SN = device.DeviceType + device.ProductCode + device.HW_PROVINCE_NAME;

                        var selectOrder = MethodWcf.SelectOrderClient(order);
                        if (selectOrder.result)
                        {
                            order = selectOrder.value[0];
                        }
                        else
                        {
                            order.Clear();
                            order.SetUpdate();
                            order.ORDER_SN = order.ORDER_SN;
                            order.PRODUCT_CODE = device.ProductCode;
                            order.PROVINCE = device.HW_PROVINCE_NAME;
                            order.DEVICE_TYPE = device.DeviceType;
                            order.NUMBER = 0;

                            if (device.HW_PROVINCE_NAME.Contains("HN") || device.HW_PROVINCE_NAME.Contains("GX"))
                                order.PRODUCT_NUMBER = 400;
                            else if (device.HW_PROVINCE_NAME.Contains("SC"))
                                order.PRODUCT_NUMBER = 480;
                            else
                                order.PRODUCT_NUMBER = 560;

                            order.FINISHED = false;
                            order.DROPED = false;

                            var insertOrder = MethodWcf.InsertOrderClient(order);
                            if (insertOrder.result==false)
                            {
                                MessageBox.Show("创建订单失败，订单号：" + order.ORDER_SN);
                                return;
                            }
                        }
                    }
                    else
                    {
                        //判断device的类型、型号、产品编码是否和当前订单一致
                        if (order.PRODUCT_CODE != device.ProductCode || order.DEVICE_TYPE != device.DeviceType || order.PROVINCE != device.HW_PROVINCE_NAME)
                        {
                            MessageBox.Show("传入数据与订单数据不一致，请检查ProductCode、DeviceType、Province等列，MAC："+device.MACAddr);
                            AppendText(this.richTextBoxFail,"传入数据与订单数据不一致，请检查ProductCode、DeviceType、Province等列，MAC：" + device.MACAddr);
                            return;
                        }
                    }

                    //判断MAC是否已经上传成功了，并且省份和类型与当前一致
                    deviceWhere.SetWhere();
                    deviceWhere.SetWhere(string.Format("MACAddr ='{0}' AND ORDER_SN='{1}' AND SN is null", device.MACAddr, order.ORDER_SN));
                    var selectDeviceCount = MethodWcf.SelectDeviceDataCountClient(deviceWhere);

                    //修改ITV_SSID和ITV_Password，只有移动和电信修改，联通不管

                    int index = device.WIFI_SSID.IndexOf('-');

                    if (device.WIFI_SSID.Contains("CMCC") && index != -1)//移动
                    {
                        device.ITV_SSID = "CMCC-share" + device.WIFI_SSID.Substring(index);

                        device.ITV_Password = device.WIFI_Password;
                    }
                    else if (device.WIFI_SSID.Contains("ChinaNet") && index != -1)//电信
                    {
                        device.ITV_SSID = "iTV" + device.WIFI_SSID.Substring(index);

                        device.ITV_Password = device.WIFI_Password;
                    }

                    device.date1 = DateTime.Now.Date;
                    device.ORDER_SN = order.ORDER_SN;

                    //更新或者写入DEVICE
                    ResultSet updateDeviceData = null;

                    //判断数据是否已经存在
                    if (selectDeviceCount.result && selectDeviceCount.message == "1")
                    {
                        //更新
                        device.SetWhere();
                        device.MACAddr = device.MACAddr;
                        AppendText(this.richTextBoxPass, "更新MAC：" + device.MACAddr);
                        updateDeviceData = MethodWcf.UpdateDeviceDataClient(device);
                    }
                    else
                    {
                        //插入
                        updateDeviceData = MethodWcf.InsertDeviceDataClient(device);
                        AppendText(this.richTextBoxPass, "添加MAC：" + device.MACAddr);
                    }

                    if (updateDeviceData.result)
                    {
                        pass++;
                    }
                    else
                    {
                        AppendText(this.richTextBoxFail, "导入失败,MAC：" + device.MACAddr + ",错误信息：" + msg + updateDeviceData.message);
                    }

                    //写回显示信息
                    total++;

                    SetText(labelPass, pass.ToString());
                    SetText(labelTotal, total.ToString());
                }

                //查询订单中的MAC数量
                deviceWhere.Clear();
                deviceWhere.SetWhere();
                deviceWhere.ORDER_SN = order.ORDER_SN;
                var selectOrderCount = MethodWcf.SelectDeviceDataCountClient(deviceWhere);
                int orderNumber = 0;
                if (selectOrderCount.result && int.TryParse(selectOrderCount.message, out orderNumber))
                {
                    AppendText(this.richTextBoxPass, "查询订单MAC数量成功");
                }
                else
                {
                    AppendText(this.richTextBoxFail, "查询订单MAC数量失败");
                    //MessageBox.Show("请检查订单数量是否正确");
                }

                //更新order信息
                order.Clear();
                order.SetWhere();
                order.ORDER_SN = order.ORDER_SN;
                order.SetUpdate();
                order.NUMBER = orderNumber;

                MethodWcf.UpdateOrderClient(order);//更新ORDER信息

                TimeSpan runTime = DateTime.Now - start;

                MessageBox.Show($"导入完成，订单数量为 {orderNumber}, run time is: {runTime.TotalSeconds}s");

                SetEnable(true);
            });
        }

        private void InsertDeviceDataThird(YcdExcelBaseClass<Device_Data> excel)
        {
            Task.Factory.StartNew(() =>
            {
                SetEnable(false);

                DateTime start = DateTime.Now;
                int total = 0, pass = 0;

                //订单信息
                YCD_ORDER order = null;

                //判断Mac是否已经上传过了的变量
                Device_Data deviceWhere = new Device_Data();

                Device_Data device = null;

                StringBuilder sqlBuf = new StringBuilder();
                sqlBuf.AppendLine(SharedData.SQL_TRANSCATION_START);
                int sqlCount = 0;

                while (true)
                {
                    #region use transaction

                    device = excel.GetRow(out string msg);

                    if(sqlCount>=100 || (device == null && sqlCount > 0))
                    {
                        sqlBuf.AppendLine(SharedData.SQL_TRANSCATION_END);
                        int executeNum = MethodWcf.ExecuteSQLClient(sqlBuf.ToString());

                        //out put result message
                        if (executeNum <= 0)
                        {
                            AppendText(this.richTextBoxFail, sqlBuf.ToString());
                        }
                        else
                        {
                            pass += executeNum;
                        }

                        total += sqlCount;

                        SetText(labelPass, pass.ToString());
                        SetText(labelTotal, total.ToString());

                        //reset properties
                        sqlCount = 0;
                        sqlBuf.Clear();

                        sqlBuf.AppendLine(SharedData.SQL_TRANSCATION_START);
                    }

                    if (device == null) break;
                    sqlCount++;

                    //判断数据与order是否是同类型的
                    if (order == null)
                    {
                        order = new YCD_ORDER();
                        order.SetWhere();
                        order.ORDER_SN = device.DeviceType + device.ProductCode + device.HW_PROVINCE_NAME;

                        var selectOrder = MethodWcf.SelectOrderClient(order);
                        if (selectOrder.result)
                        {
                            order = selectOrder.value[0];
                        }
                        else
                        {
                            order.Clear();
                            order.SetUpdate();
                            order.ORDER_SN = order.ORDER_SN;
                            order.PRODUCT_CODE = device.ProductCode;
                            order.PROVINCE = device.HW_PROVINCE_NAME;
                            order.DEVICE_TYPE = device.DeviceType;
                            order.NUMBER = 0;

                            if (device.HW_PROVINCE_NAME.Contains("HN")
                            || device.HW_PROVINCE_NAME.Contains("GX")
                            || device.HW_PROVINCE_NAME.Contains("FJ"))
                                order.PRODUCT_NUMBER = 400;
                            else if (device.HW_PROVINCE_NAME.Contains("SC"))
                                order.PRODUCT_NUMBER = 480;
                            else
                                order.PRODUCT_NUMBER = 560;

                            order.FINISHED = false;
                            order.DROPED = false;

                            var insertOrder = MethodWcf.InsertOrderClient(order);
                            if (insertOrder.result == false)
                            {
                                MessageBox.Show("创建订单失败，订单号：" + order.ORDER_SN);
                                return;
                            }
                        }
                    }
                    else
                    {
                        //判断device的类型、型号、产品编码是否和当前订单一致
                        if (order.PRODUCT_CODE != device.ProductCode || order.DEVICE_TYPE != device.DeviceType || order.PROVINCE != device.HW_PROVINCE_NAME)
                        {
                            MessageBox.Show("传入数据与订单数据不一致，请检查ProductCode、DeviceType、Province等列，MAC：" + device.MACAddr);
                            AppendText(this.richTextBoxFail, "传入数据与订单数据不一致，请检查ProductCode、DeviceType、Province等列，MAC：" + device.MACAddr);
                            return;
                        }
                    }

                    //append insert or update sql statement
                    deviceWhere.Clear();
                    deviceWhere.SetWhere(string.Format("MACAddr ='{0}' AND ORDER_SN='{1}' AND SN is null", device.MACAddr, order.ORDER_SN));

                    sqlBuf.AppendLine($"IF ({Shared.Method.ObjectToSQL.ObjectToSelectCountSql(deviceWhere)}) = 0 BEGIN");

                    int index = device.WIFI_SSID.IndexOf('-');

                    if (device.WIFI_SSID.Contains("CMCC") && index != -1)//移动
                    {
                        device.ITV_SSID = "CMCC-share" + device.WIFI_SSID.Substring(index);

                        device.ITV_Password = device.WIFI_Password;
                    }
                    else if (device.WIFI_SSID.Contains("ChinaNet") && index != -1)//电信
                    {
                        device.ITV_SSID = "iTV" + device.WIFI_SSID.Substring(index);

                        device.ITV_Password = device.WIFI_Password;
                    }

                    device.date1 = DateTime.Now.Date;
                    device.ORDER_SN = order.ORDER_SN;
                    //insert
                    sqlBuf.AppendLine(Shared.Method.ObjectToSQL.ObjectToInsertSql(device));
                    sqlBuf.AppendLine("END ELSE BEGIN");

                    device.SetWhere();
                    device.MACAddr = device.MACAddr;
                    //update
                    sqlBuf.AppendLine(Shared.Method.ObjectToSQL.ObjectToUpdateSql(device));
                    sqlBuf.AppendLine("END");

                    //continue;

                    #endregion
#if false
                    //判断MAC是否已经上传成功了，并且省份和类型与当前一致
                    deviceWhere.SetWhere();
                    deviceWhere.SetWhere(string.Format("MACAddr ='{0}' AND ORDER_SN='{1}' AND SN is null", device.MACAddr, order.ORDER_SN));
                    var selectDeviceCount = MethodWcf.SelectDeviceDataCountClient(deviceWhere);

                    //修改ITV_SSID和ITV_Password，只有移动和电信修改，联通不管

                    int index = device.WIFI_SSID.IndexOf('-');

                    if (device.WIFI_SSID.Contains("CMCC") && index != -1)//移动
                    {
                        device.ITV_SSID = "CMCC-share" + device.WIFI_SSID.Substring(index);

                        device.ITV_Password = device.WIFI_Password;
                    }
                    else if (device.WIFI_SSID.Contains("ChinaNet") && index != -1)//电信
                    {
                        device.ITV_SSID = "iTV" + device.WIFI_SSID.Substring(index);

                        device.ITV_Password = device.WIFI_Password;
                    }

                    device.date1 = DateTime.Now.Date;
                    device.ORDER_SN = order.ORDER_SN;

                    //更新或者写入DEVICE
                    ResultSet updateDeviceData = null;

                    //判断数据是否已经存在
                    if (selectDeviceCount.result && selectDeviceCount.message == "1")
                    {
                        //更新
                        device.SetWhere();
                        device.MACAddr = device.MACAddr;
                        AppendText(this.richTextBoxPass, "更新MAC：" + device.MACAddr);
                        updateDeviceData = MethodWcf.UpdateDeviceDataClient(device);
                    }
                    else
                    {
                        //插入
                        updateDeviceData = MethodWcf.InsertDeviceDataClient(device);
                        AppendText(this.richTextBoxPass, "添加MAC：" + device.MACAddr);
                    }

                    if (updateDeviceData.result)
                    {
                        pass++;
                    }
                    else
                    {
                        AppendText(this.richTextBoxFail, "导入失败,MAC：" + device.MACAddr + ",错误信息：" + msg + updateDeviceData.message);
                    }

                    //写回显示信息
                    total++;

                    SetText(labelPass, pass.ToString());
                    SetText(labelTotal, total.ToString());
#endif
                }

                //查询订单中的MAC数量
                deviceWhere.Clear();
                deviceWhere.SetWhere();
                deviceWhere.ORDER_SN = order.ORDER_SN;
                var selectOrderCount = MethodWcf.SelectDeviceDataCountClient(deviceWhere);
                int orderNumber = 0;
                if (selectOrderCount.result && int.TryParse(selectOrderCount.message, out orderNumber))
                {
                    AppendText(this.richTextBoxPass, "查询订单MAC数量成功");
                }
                else
                {
                    AppendText(this.richTextBoxFail, "查询订单MAC数量失败");
                    //MessageBox.Show("请检查订单数量是否正确");
                }

                //更新order信息
                order.Clear();
                order.SetWhere();
                order.ORDER_SN = order.ORDER_SN;
                order.SetUpdate();
                order.NUMBER = orderNumber;

                MethodWcf.UpdateOrderClient(order);//更新ORDER信息

                TimeSpan runTime = DateTime.Now - start;

                MessageBox.Show($"导入完成，订单总数为 {orderNumber}, run time is: {runTime.TotalSeconds}s");

                SetEnable(true);
            });
        }

        //导入途中，任何按键都不能使用
        //导入完成，按键都可以使用了

        private void SetEnable(bool isWait)
        {
            if (this.InvokeRequired)
            {
                Action<bool> d = new Action<bool>(SetEnable);
                Invoke(d, isWait);
            }
            else
            {
                //this.textBoxOrderSn.Enabled = isWait;
                this.comboBoxTableName.Enabled = isWait;
                this.buttonChoiceExcel.Enabled = isWait;
                this.buttonExcelInput.Enabled = isWait;
            }
        }

        //显示导入成功的数字
        private void SetText(Control formObj, string msg)
        {
            if (formObj.InvokeRequired)
            {
                Action<Control, string> d = new Action<Control, string>(SetText);
                Invoke(d, formObj, msg);
            }
            else
            {
                formObj.Text = msg;
            }
        }
        //添加信息

        private void AppendText(RichTextBox box, string msg)
        {
            if (box.InvokeRequired)
            {
                Action<RichTextBox, string> d = new Action<RichTextBox, string>(AppendText);
                Invoke(d, box, msg);
            }
            else
            {
                if (msg == null) return;
                if (box.Text != null && box.TextLength >= 10000)
                    box.Clear();
                box.AppendText(msg);
                box.AppendText("\n");
            }
        }

        //滚动显示
        private void RichTextBoxPass_TextChanged(object sender, EventArgs e)
        {
            richTextBoxPass.SelectionStart = richTextBoxPass.Text.Length;
            richTextBoxPass.ScrollToCaret();
        }

        //滚动显示错误信息
        private void RichTextBoxFail_TextChanged(object sender, EventArgs e)
        {
            richTextBoxFail.SelectionStart = richTextBoxFail.Text.Length;
            richTextBoxFail.ScrollToCaret();
        }

        private void FormInputData_Load(object sender, EventArgs e)
        {
            return;

            //加载BOSA TYPE
            var bobTypes = MethodWcf.SelectJsonClient<List<BOB_TYPE>>("SELECT * FROM Modem.dbo.BOB_TYPE");

            if (bobTypes != null && bobTypes.Count > 0)
            {
                _listBobType = bobTypes;
                var companys = bobTypes.Select(a => a.COMPANY).Distinct().ToArray();
                this.comboBoxBosaCompany.Items.AddRange(companys);
            }
            else
            {
                MessageBox.Show("未能加载BOB类型信息");
                Application.Exit();
            }
        }

    }
}
